#!/bin/bash
# date: 2022/11/22
# auth: frealess11
# desc: install webhook
# https://github.com/prometheus/alertmanager/blob/main/examples/webhook/echo.go

ip_local=$(/usr/sbin/ip add | grep inet | grep global | grep -Ev 'docker' | awk '{print $2}' | awk -F/ '{print $1}' | tail -1)
port=5001
addr=${ip_local}:${port}
docker_name="webhook_${port}"
image="11expose11/webhook:v0.1"

docker_start() {
  docker run -d \
    --name ${docker_name} \
    -m 800m \
    --net=host \
    --restart=always \
    --log-opt max-size=100m \
    --log-opt max-file=10 \
    ${image}
}

docker_stop() {
  docker stop ${docker_name}
  docker rm ${docker_name}
}

docker_exec() {
  docker exec -it ${docker_name} sh
}

docker_logs() {
  docker logs --tail=10 ${docker_name}
}

send() {
  curl -XPOST http://${addr} -d'{ "message":"hello world" }'
}

case $1 in
start)
  docker_start
  ;;
stop)
  docker_stop
  ;;
exec)
  docker_exec
  ;;
logs)
  docker_logs
  ;;
send)
  send
  ;;
*)
  echo "$0 start|stop|exec|logs|send"
  ;;
esac
